/**
 * 
 */
package controller.xml;

import model.preprocessor.PreProcessorParameter;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Use to parse a {@link PreProcessorParameter} in the exported session in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class PreProcessorParameterXMLHandler extends XMLHandler {

	private PreProcessorParameter			param	= null;
	private PreProcessorParameterXMLStatus	status	= PreProcessorParameterXMLStatus.NONE;

	// private BasicParameterXMLHandler basicHandler = null;

	public PreProcessorParameterXMLHandler() {
	}

	@Override
	public boolean characters2(char[] ch, int start, int length) throws SAXException {
		throw new SAXException("unnexpecte behavior");
	}

	@Override
	public boolean endDocument2() throws SAXException {
		return false;
	}

	@Override
	public boolean endElement2(String uri, String localName, String qName) throws SAXException {
		switch (status) {
		case NONE:
			return false;

		case BASIC_PARAMETER:
			if (qName.equals(XMLBaliseFormat.BASIC_PARAMETER))
			{// ferme basic param
				status = PreProcessorParameterXMLStatus.NONE;
				param = new PreProcessorParameter();
				param.setTimeOut(((BasicParameterXMLHandler) delegate).getTimeout());
				delegate = null;
				return true;
			}
			throw new SAXException("end of BASIC PARAM was expected");
		}

		// attend une fin de balise qui existe pas...
		throw new SAXException("an other end of element was expected");
	}

	@Override
	public boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (status != PreProcessorParameterXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.BASIC_PARAMETER))
		{// balise basic
			// set les donne pour recupere le basic result
			status = PreProcessorParameterXMLStatus.BASIC_PARAMETER;
			delegate = new BasicParameterXMLHandler();

			return true;
		}

		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Return the param.
	 * 
	 * @return the param
	 */
	public PreProcessorParameter getParam() {
		return param;
	}


}
